package com.paobuji.aidebao.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.bean.GetSportHistoryList;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created by lenovo on 2017/3/16.
 */

public class MyHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mInflater;
    private ArrayList activityList;
    Context context;
    DecimalFormat decimalFormat;
    /**
     * 构造器
     *
     * @param context
     */
    public MyHistoryAdapter(Context context, ArrayList activityList) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.activityList = activityList;
        decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
    }

    /**
     * item显示类型
     * 引入布局
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //进行判断显示类型，来创建返回不同的View
        View view = LayoutInflater.from(context).inflate(R.layout.item_myhistory, parent, false);
        //view.setBackgroundColor(Color.RED);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        //绑定事件
        ((ItemViewHolder) holder).sportDate.setText(((GetSportHistoryList.DataBean.SporthitorylistBean)activityList.get(position)).getSportDate());
        ((ItemViewHolder) holder).sportCor.setText(((GetSportHistoryList.DataBean.SporthitorylistBean)activityList.get(position)).getAllCor()+context.getString(R.string.calorie));
        ((ItemViewHolder) holder).sportTime.setText(getTime(((GetSportHistoryList.DataBean.SporthitorylistBean)activityList.get(position)).getSportTime())+"");
        ((ItemViewHolder) holder).sportDis.setText(decimalFormat.format(((GetSportHistoryList.DataBean.SporthitorylistBean)activityList.get(position)).getAlldis())+context.getString(R.string.unit));

    }

    /**
     * 要显示的item数目
     *
     * @return
     */
    @Override
    public int getItemCount() {

        //由于添加了footer所以返回的值都要加1个
        return activityList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView sportDate;
        public TextView sportCor;
        public TextView sportTime;
        public TextView sportDis;

        public ItemViewHolder(View view) {
            super(view);
            sportDate = (TextView) view.findViewById(R.id.sportDate);
            sportCor = (TextView) view.findViewById(R.id.corText);
            sportTime = (TextView) view.findViewById(R.id.timeText);
            sportDis = (TextView) view.findViewById(R.id.disText);
        }
    }

    public static String getTime(int second) {
        if (second < 10) {
            return "00:0" + second;
        }
        if (second < 60) {
            return "00:" + second;
        }
        if (second < 3600) {
            int minute = second / 60; second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + ":0" + second;
                } return "0" + minute + ":" + second;
            }
            if (second < 10) {
                return minute + ":0" + second;
            }
            return minute + ":" + second;
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + ":0" + minute + ":0" + second;
                }
                return "0" + hour + ":0" + minute + ":" + second;
            }
            if (second < 10) {
                return "0" + hour + minute + ":0" + second;
            } return "0" + hour + minute + ":" + second;
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + ":0" + minute + ":0" + second;
            }
            return hour + ":0" + minute + ":" + second;
        }
        if (second < 10) {
            return hour + minute + ":0" + second;
        }
        return hour + minute + ":" + second;
    }
}
